#include "Game.h"
#include "MapManager.h"
#include "external/tinyxml.h"
#include "NPC.h"
#include "LuaTrigger.h"
using namespace std;

MapManager::MapManager(void)
	{
		actual = NULL;
		G_EVENTOS->addListener(TRIGGER_ENTRADA,this);
	}


void MapManager::cargarMapa( string & nombreMapa )
	{
		string nombreM(nombreMapa);
		if (actual){
			for (unsigned int a = 0; a < triggers.size(); a++)
				delete triggers.at(a);
			triggers.clear();
			delete actual;
		}
		actual = new Mapa("data/maps/" + nombreM);
		
		G_EVENTOS->enviarEvento(new Event(CARGANDO_MAPA,new eventoCargaMapa(actual)));
		//cargamos los triggers

		TiXmlDocument mapaXML(("data/maps/" + nombreM).c_str());
		
		if (!mapaXML.LoadFile())
			g->alertaError(nombreM + " no encontrado.");
		
		TiXmlElement *objectGroup = mapaXML.FirstChildElement("map")->FirstChildElement("objectgroup");
		TiXmlElement *trigger 	= NULL;
		TiXmlElement *propiedad = NULL;
		TiXmlElement *npc		= NULL;

		//buscamos el object group de triggers

		while(objectGroup){
			if (!strcmp(objectGroup->Attribute("name"),"triggers"))
				break;
			objectGroup = objectGroup->NextSiblingElement("objectgroup");
		}

		if (objectGroup){//si existe la capa de triggers, vemos que tipo es
			trigger = objectGroup->FirstChildElement("object");
			while (trigger){//por cada trigger
				string nombreTrigger	= trigger->Attribute("name");
				string tipoTrigger		= trigger->Attribute("type");

				float x1 = atof(trigger->Attribute("x"));
				float y1 = atof(trigger->Attribute("y"));

				float x2 = atof(trigger->Attribute("width")) + x1;
				float y2 = atof(trigger->Attribute("height")) + y1;

				//dependiendo del tipo creamos un objeto u otro.
				Trigger *nuevoTrigger = NULL;

				if (!tipoTrigger.compare("entrada")){//si es del tipo entrada
					propiedad = trigger->FirstChildElement("properties")->FirstChildElement("property");
					string destino,spawn;
					while (propiedad){

						string name = propiedad->Attribute("name");						
						
						if (!name.compare("destino"))
							destino = propiedad->Attribute("value");
						else if (!name.compare("spawn"))
							spawn = propiedad->Attribute("value");
						
						propiedad = propiedad->NextSiblingElement("property");
					}

					nuevoTrigger = new TriggerEntrada(destino,spawn,nombreTrigger,x1,y1,x2,y2);
	
				}else if (!tipoTrigger.compare("lua")){//otros tipos de triggers...trigger lua y tal.
					propiedad = trigger->FirstChildElement("properties")->FirstChildElement("property");
					string nombreLua = propiedad->Attribute("value");
					nuevoTrigger = new LuaTrigger(nombreTrigger,x1,y1,x2,y2,nombreLua);
				}
				triggers.push_back(nuevoTrigger);

				trigger = trigger->NextSiblingElement("object");
			}
		}

		//Spawns
		objectGroup = mapaXML.FirstChildElement("map")->FirstChildElement("objectgroup");

		while(objectGroup){
			if (!strcmp(objectGroup->Attribute("name"),"spawns"))
				break;
			objectGroup = objectGroup->NextSiblingElement("objectgroup");
			}

		if (objectGroup){
			TiXmlElement *spawn;
			spawn = objectGroup->FirstChildElement("object");
			while(spawn){
				string nombre = spawn->Attribute("name");
				float x = atof(spawn->Attribute("x"));
				float y = atof(spawn->Attribute("y"));
				actual->addSpawnPoint(x,y,nombre);
				spawn = spawn->NextSiblingElement("object");
			}
		}

		//buscamos el object group de npcs

		objectGroup = mapaXML.FirstChildElement("map")->FirstChildElement("objectgroup");

		while(objectGroup){
			if (!strcmp(objectGroup->Attribute("name"),"npcs"))
				break;
			objectGroup = objectGroup->NextSiblingElement("objectgroup");
			}
			
		if (objectGroup){//si existe un grupo de npcs
			npc = objectGroup->FirstChildElement("object");

			float x = atof(npc->Attribute("x"));
			float y = atof(npc->Attribute("y"));
			
			x = x - ((int)x)%40;
			y = y - ((int)y)%40;
			string nombreNPC = npc->Attribute("name");

			g->addActor(new NPC(nombreNPC,x,y));

		}
	}

void MapManager::comprobarTriggers( float x, float y )
	{
	for (unsigned int a = 0; a < triggers.size(); a++)
		triggers[a]->comprobar(x,y);
	}
void MapManager::renderTriggers(){
	for (unsigned int a = 0; a < triggers.size(); a++)
		triggers[a]->render();
	}
bool MapManager::onEvent( Event *e )
	{
	//por ahora solo controlamos el evento de trigger de entrada a zona
	eventoTriggerEntrada *eventData = (eventoTriggerEntrada*)e->datos;
	cargarMapa(eventData->t->getDestino());
	return false;
	}

MapManager::~MapManager(void)
	{
	}
